package com.ruoyi.bizsys.domain;

import java.math.BigDecimal;

import com.ruoyi.common.annotation.Excel;
import com.fasterxml.jackson.annotation.JsonFormat;

import java.util.Date;

import lombok.Builder;
import lombok.Data;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.IdType;
import com.ruoyi.common.core.domain.BaseEntity;
import lombok.experimental.Accessors;

/**
 * 借款申请对象 loan_order
 *
 * @author african-loan
 * @date 2021-12-30
 */

@Data
public class LoanOrder extends BaseEntity {
    private static final long serialVersionUID = 1L;

    public static final int LOAN_STATUS_9_APPROVALING = 9;
    public static final int LOAN_STATUS_10_CANCEL = 10;
    public static final int LOAN_STATUS_0_APPROVE_REFUSED = 0;
    public static final int LOAN_STATUS_1_APPROVE_PASSED = 1;
    public static final int LOAN_STATUS_2_INSUFFICIENT_ACCOUNT_BALANCE = 2;
    public static final int LOAN_STATUS_3_LOAN_PENDING = 3;
    public static final int LOAN_STATUS_4_LOAN_SUCCESS = 4;
    public static final int LOAN_STATUS_5_LOAN_FAILED = 5;
    public static final int LOAN_STATUS_6_RE_INITIATE_PAY = 6;
    public static final int LOAN_STATUS_7_CONTRAC_GENERATED = 7;
    public static final int LOAN_STATUS_8_CLOSE = 8;

    /**
     * PK
     */

    @Excel(name = "${comment}", readConverterExp = "PK")
    @TableId(type = IdType.AUTO)
    private Long id;


    /**
     * 借款流水号
     */

    @Excel(name = "借款流水号")
    private String loanSerialNo;


    /**
     * 客户号
     */

    @Excel(name = "客户号")
    private String custNo;


    /**
     * 产品ID
     */

    @Excel(name = "产品ID")
    private Long fkProductId;


    /**
     * 产品编码
     */

    @Excel(name = "产品编码")
    private String productCode;


    /**
     * 产品名称
     */

    @Excel(name = "产品名称")
    private String productName;


    /**
     * 产品期数
     */

    @Excel(name = "产品期数")
    private Integer periodNum;


    /**
     * 单位
     */

    @Excel(name = "单位")
    private String unit;


    /**
     * 服务费率
     */

    @Excel(name = "服务费率")
    private BigDecimal serviceFeeRate;


    /**
     * 利(息)率
     */

    @Excel(name = "利(息)率")
    private BigDecimal interestRate;


    /**
     * 服务费
     */

    @Excel(name = "服务费")
    private BigDecimal serviceFee;


    /**
     * 趸交
     */

    @Excel(name = "趸交")
    private String isSinglePayment;


    /**
     * 最低贷款额度
     */

    @Excel(name = "最低贷款额度")
    private BigDecimal minQuota;


    /**
     * 最高贷款额度
     */

    @Excel(name = "最高贷款额度")
    private BigDecimal maxQuota;


    /**
     * 滞纳金日费率
     */

    @Excel(name = "滞纳金日费率")
    private BigDecimal dateLateFee;


    /**
     * 滞纳金按日收费宽限期（天数）
     */

    @Excel(name = "滞纳金按日收费宽限期", readConverterExp = "天=数")
    private Integer lateFeeGracePeriod;


    /**
     * 滞纳金计算最大天数
     */

    @Excel(name = "滞纳金计算最大天数")
    private Integer lateFeeMaxDays;


    /**
     * 借款额度
     */

    @Excel(name = "借款额度")
    private BigDecimal loanAmt;


    /**
     * 银行卡
     */

    @Excel(name = "银行卡")
    private String accountNumber;


    /**
     * 借款期数
     */

    @Excel(name = "借款期数")
    private String tenor;


    /**
     * 贷款用途
     */

    @Excel(name = "贷款用途")
    private String loanPurpose;


    /**
     * 贷款用途其他
     */

    @Excel(name = "贷款用途其他")
    private String loanPurposeOther;


    /**
     * 应还利息
     */

    @Excel(name = "应还利息")
    private BigDecimal interestAmt;


    /**
     * 应还总金额
     */

    @Excel(name = "应还总金额")
    private BigDecimal totalAmt;


    /**
     * 借款状态
     */

    @Excel(name = "借款状态")
    private Integer loanStatus;


    /**
     * 还款日期
     */

    @Excel(name = "还款日期")
    private String repayDate;


    /**
     * 合同编号
     */

    @Excel(name = "合同编号")
    private String contractNo;


    /**
     * 放款流水号
     */

    @Excel(name = "放款流水号")
    private String payTransSerialNo;

    /**
     * 转帐收据code
     */
    private String recipientCode;


    /**
     * 状态
     */

    @Excel(name = "状态")
    private String state;


    /**
     * 备注
     */

    @Excel(name = "备注")
    private String remark;


    /**
     * 创建者
     */

    @Excel(name = "备注")
    private String createBy;


    /**
     * 冻结期
     */

    @Excel(name = "冻结期")
    private String freezeDate;


    /**
     * 创建时间
     */

    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    @Excel(name = "冻结期", width = 30, dateFormat = "yyyy-MM-dd HH:mm:ss")
    private Date createTime;


    /**
     * 更新者
     */

    @Excel(name = "冻结期")
    private String updateBy;


    /**
     * 更新时间
     */

    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    @Excel(name = "冻结期", width = 30, dateFormat = "yyyy-MM-dd HH:mm:ss")
    private Date updateTime;

    /**
     * 是否发送短信
     */
    private Integer sendSms;

    public LoanOrder chainId(Long id){
        this.id = id;
        return this;
    }

    public LoanOrder chainRemark(String remark){
        this.remark = remark;
        return this;
    }

    public LoanOrder chainLoanStatus(Integer loanStatus){
        this.loanStatus = loanStatus;
        return this;
    }

    public LoanOrder chainFreezeDate(String freezeDate){
        this.freezeDate = freezeDate;
        return this;
    }

    public LoanOrder chainContractNo(String contractNo){
        this.contractNo = contractNo;
        return this;
    }

    /**
     * 根据产品信息计算相应金额
     * @param loanAmt
     * @param product
     */
    public void generateOrderAmount(BigDecimal loanAmt, ProductInfo product) {
        //服务费
        BigDecimal serviceFee = loanAmt.multiply(product.getServiceFee());
        this.setServiceFee(serviceFee);
        BigDecimal interestAmt = loanAmt.multiply(product.getInterestRate()).multiply(BigDecimal.valueOf(product.getPeriodNum()));
        //应还利息
        this.setInterestAmt(interestAmt);
        //应还总金额
        this.setTotalAmt(loanAmt.add(interestAmt).add(serviceFee));
    }
}